package com.zhoug.zmui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.NumberPicker;

import com.zhoug.common.compat.NumberPickerCompat;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.ResourceUtils;

/**
 * 数字选择器 支持修改分割线和文字样式
 *
 * @Author 35574
 * @Date 2022/3/7
 * @Description
 */
public class ZMUINumberPicker extends NumberPicker {
    private static final String TAG = ">>>ZMUINumberPicker";
    private static boolean debug = false;



    public ZMUINumberPicker(Context context) {
        super(context);
        init(context, null, 0);
    }

    public ZMUINumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public ZMUINumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZMUINumberPicker, defStyleAttr, R.style.zmui_style_ZMUINumberPicker);
        int textColor = a.getColor(R.styleable.ZMUINumberPicker_android_textColor, 0xff333333);
        float textSize = a.getDimension(R.styleable.ZMUINumberPicker_android_textSize, ResourceUtils.spTopx(14));
        Drawable dividerLine = a.getDrawable(R.styleable.ZMUINumberPicker_zmui_dividerLine);
        int dividerLineHeight = a.getDimensionPixelOffset(R.styleable.ZMUINumberPicker_zmui_dividerLineHeight, 2);
        a.recycle();
        if (debug) {
            LogUtils.d(TAG, "init:dividerLine=" + textColor);
            LogUtils.d(TAG, "init:dividerLine=" + dividerLine);
            LogUtils.d(TAG, "init:dividerLineHeight=" + dividerLineHeight);
        }
        if(dividerLine!=null){
            setSelectionDivider(dividerLine);
        }
        setSelectionDividerHeight(dividerLineHeight);
        setTextColor(textColor);
        if(textSize>0){
            setTextSize(textSize);
        }
    }



    /**
     * 设置文字颜色
     *
     * @param textColor
     */
    @Override
    public void setTextColor(int textColor) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            super.setTextColor(textColor);
        } else {
            NumberPickerCompat.setTextColor(this, textColor);
        }
    }

    /**
     * 设置文字大小 px
     * @param textSize
     */
    public  void setTextSize( float textSize) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            super.setTextSize(textSize);
        }else{
            NumberPickerCompat.setTextSize(this, textSize);
        }
    }

    /**
     * 设置分割线颜色
     *
     * @param dividerColor
     */
    @Deprecated
    public void setDividerLineColor(int dividerColor) {
        setSelectionDivider(dividerColor);
    }


    /**
     * 利用反射设置分割线
     *
     * @param dividerLine
     * @see #setSelectionDivider(Drawable)
     */
    @Deprecated
    public void setDividerLine(Drawable dividerLine) {
        setSelectionDivider(dividerLine);
    }

    /**
     * 设置分割线高度
     *
     * @param height 分割线高度
     * @see #setSelectionDividerHeight
     * @deprecated
     */
    @Deprecated
    public void setDividerLineHeight(int height) {
        if (height < 0) return;
        setSelectionDividerHeight(height);
    }

    /**
     * 设置分割线
     *
     * @param dividerColor
     */
    public void setSelectionDivider(int dividerColor) {
        NumberPickerCompat.setSelectionDivider(this, new ColorDrawable(dividerColor));
    }

    /**
     * 设置分割线
     *
     * @param divider
     */
    public void setSelectionDivider(Drawable divider) {
        NumberPickerCompat.setSelectionDivider(this, divider);
    }

    /**
     * 设置分割线高度
     * @param height 高度
     */
    @Override
    public void setSelectionDividerHeight(int height) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            super.setSelectionDividerHeight(height);
        } else {
            NumberPickerCompat.setSelectionDividerHeight(this, height);
        }
    }

}
